﻿#include "recboxitem.h"
#include "ui_recboxitem.h"
#include <QPropertyAnimation>

RecBoxItem::RecBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);
    ui->musicImageBox->installEventFilter(this);
}

RecBoxItem::~RecBoxItem()
{
    delete ui;
}

bool RecBoxItem::eventFilter(QObject* watched, QEvent* event) {
    if (watched == ui->musicImageBox) {
        if (QEvent::Enter == event->type())
        {
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry");
            animation->setDuration(150);
            animation->setStartValue(QRect(11,11, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->setEndValue(QRect(11, 0, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->start();

            connect(animation, &QPropertyAnimation::finished, this, [=]() {
                delete animation;
                });
        }
        if (QEvent::Leave == event->type())
        {
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry");
            animation->setDuration(150);
            animation->setStartValue(QRect(11, 0, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->setEndValue(QRect(11, 11, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->start();

            connect(animation, &QPropertyAnimation::finished, this, [=]() {
                delete animation;
                });
        }
    }

    return QWidget::eventFilter(watched, event);
}

void RecBoxItem::setRecText(const QString& text) {
    ui->recBoxItemText->setText(text);
}

void RecBoxItem::setRecImage(const QString& imagePath) {
    QString style = "background-image:url(" + imagePath + ");";
    ui->recMusicImage->setStyleSheet(style);
}
